using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace Clock.Utils
{
	public class OsUtil
	{

		protected static bool IsLinux
		{
			get
			{
				int p = (int) Environment.OSVersion.Platform;
				return (p == 4) || (p == 6) || (p == 128);
			}
		}

        protected static void ProcessCommand(string FileToExecute, string Arguments)
        {
            Process p = new Process();
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = FileToExecute;
            info.UseShellExecute = false;
            info.RedirectStandardError = true;
            info.RedirectStandardOutput = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            info.CreateNoWindow = true;
            //-t .75
            info.Arguments = Arguments;
            p.StartInfo = info;
            try
            {
                p.Start();
                p.WaitForExit();
            }
            catch (Exception x)
            {
                Debug.WriteLine(x.Message);
                throw x;
            }
        }

        protected static string GetProgramPath(string ProgramDirectoryName, string ExeName)
        {
            if (!IsLinux)
            {
                //Check PATH first
                var enviromentPath = System.Environment.GetEnvironmentVariable("PATH");

               
                var paths = enviromentPath.Split(';');
                var exePath = paths.Select(x => Path.Combine(x, ExeName))
                                   .Where(x => File.Exists(x))
                                   .FirstOrDefault();
                if (string.IsNullOrWhiteSpace(exePath) == false)
                {
                    return exePath;
                }

                //Next check program files (64bit first)
                string ProgramW6432 = Environment.GetEnvironmentVariable("ProgramW6432");
                string pgFolder = Path.Combine(ProgramW6432 == null ? string.Empty : ProgramW6432, ProgramDirectoryName);
                string pg86Folder = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), ProgramDirectoryName);

                if (ProgramDirectoryName != null && ProgramDirectoryName != "")
                {
                    if (Directory.Exists(pgFolder))
                    {
                        string[] gsfiles = Directory.GetFiles(pgFolder, ExeName, SearchOption.AllDirectories);

                        foreach (string gs in gsfiles)
                            return gs;
                    }

                    if (Directory.Exists(pg86Folder))
                    {
                        string[] gsfiles = Directory.GetFiles(pg86Folder, ExeName, SearchOption.AllDirectories);

                        foreach (string gs in gsfiles)
                            return gs;
                    }
                }
                //Finally check directory of executing assembly
                string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, ExeName, SearchOption.AllDirectories);

                foreach (string gs in files)
                    return gs;


                return null;

            }
            else
            {
                return ExeName;
            }
        }

		public OsUtil ()
		{
		}
	}
}

